{% extends 'generic/object_edit.html' %}
{% load static %}
{% load form_helpers %}

{% block form %}
    <div class="panel panel-default">
        <div class="panel-heading"><strong>Custom Field</strong></div>
        <div class="panel-body">
            {% render_field form.name %}
            {% render_field form.type %}
            {% render_field form.weight %}
            {% render_field form.label %}
            {% render_field form.description %}
            {% render_field form.required %}
            {% render_field form.default %}
            {% render_field form.filter_logic %}
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><strong>Assignment</strong></div>
        <div class="panel-body">
            {% render_field form.content_types %}
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><strong>Validation Rules</strong></div>
        <div class="panel-body">
            {% render_field form.validation_minimum %}
            {% render_field form.validation_maximum %}
            {% render_field form.validation_regex %}
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading"><strong>Custom Field Choices</strong></div>
        <div class="panel-body">
            {% if choices.errors %}
                <div class="panel panel-danger">
                    <div class="panel-heading"><strong>Errors</strong></div>
                    <div class="panel-body">
                        Please correct the error(s) below:

                        {% for choice in choices.forms %}
                            {% if choice.errors %}
                                {% for error in choice.errors.values %}{{ error }}{% endfor %}
                            {% endif %}
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
            {{ choices.non_field_errors }}
            <div class="form-group{% if field.errors %} has-error{% endif %}">
            <table class="table" id="custom-field-choices">
              {{ choices.management_form }}
              {% for choice in choices.forms %}
                    {% if forloop.first %}
                        <thead>
                        <tr>
                            {% for field in choice.visible_fields %}
                                <th>{{ field.label|capfirst }}</th>
                            {% endfor %}
                        </tr>
                        </thead>
                    {% endif %}
                    <tr class="formset_row-{{ choices.prefix }}">
                        {% for field in choice.visible_fields %}
                            <td>
                                {# Include the hidden fields in the form #}
                                {% if forloop.first %}
                                    {% for hidden in choice.hidden_fields %}
                                        {{ hidden }}
                                    {% endfor %}
                                {% endif %}
                                {{ field }}
                                {% if field.errors %}
                                    <ul>
                                        {% for error in field.errors %}
                                            {# Embed an HTML comment indicating the error for extraction by tests #}
                                            <!-- FORM-ERROR {{ field.name }}: {{ error }} -->
                                            <li class="text-danger">{{ error }}</li>
                                        {% endfor %}
                                    </ul>
                                {% endif %}
                            </td>
                        {% endfor %}
                    </tr>
               {% endfor %}
            </table>
            </div>
        </div>
    </div>
    {% include 'inc/extras_features_edit_form_fields.html' %}

{% endblock form %}

{% block javascript %}
<script src="{% static 'jquery/jquery.formset.js' %}"></script>
<script type="text/javascript">
    $('.formset_row-{{ choices.prefix }}').formset({
        addText: '<span class="mdi mdi-plus-thick" aria-hidden="true"></span> Add another Custom Field Choice',
        addCssClass: 'btn btn-primary add-row',
        deleteText: '<span class="mdi mdi-trash-can-outline" aria-hidden="true"></span>',
        deleteCssClass: 'btn btn-danger delete-row',
        prefix: '{{ choices.prefix }}',
        formCssClass: 'dynamic-formset-{{ choices.prefix }}',
        keepFieldValues: 'input[type="number"]'
    });
</script>
{% endblock javascript %}
